<template>
  <div>
    <div class="train-info">
      <div class="item">
        <div class="icon" />
        <div class="context">
          <div class="label">在线车辆数</div>
          <div class="value">{{ tainInfo.online }}</div>
        </div>
      </div>

      <div class="item">
        <div class="icon" />
        <div class="context">
          <div class="label">离线车辆数</div>
          <div class="value">{{ tainInfo.offline }}</div>
        </div>
      </div>
    </div>

    <div class="device-info">
      <div class="item">
        <div class="icon" />
        <div class="context">
          <div class="label">在线设备数</div>
          <div class="value">{{ deviceInfo.online }}</div>
        </div>
      </div>

      <div class="item">
        <div class="icon" />
        <div class="context">
          <div class="label">离线设备数</div>
          <div class="value">{{ deviceInfo.offline }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'CurrentTrain',
  data() {
    return {
      timer: null,
      tainInfo: {
        online: 0,
        offline: 0
      },
      deviceInfo: {
        online: 0,
        offline: 0
      },
      require:{
          channels: [],
          component: 'StationTrainCurrentInfo'
      },
    }
  },
  mounted() {
    this.subscribeTrainInfo()
  },
  beforeDestroy() {
    for(let i = 0;i < this.require.channels.length; i++){
        this.sockets.unsubscribe(this.require.channels[i])
    }
  },
  methods: {
    subscribeTrainInfo() {
      let that = this
      var devicestate_name='deviceState'
      if(!that.require.channels.includes(devicestate_name))
      {              
        that.require.channels.push(devicestate_name)    
        that.$bus.$emit('Subscribe', that.require)     
        that.sockets.subscribe(devicestate_name, res => {
          this.tainInfo.online = res.data.onlineTrainNumber
          this.tainInfo.offline = res.data.offlineTrainNumber
          this.deviceInfo.online = res.data.onlineHandNumber
          this.deviceInfo.offline = res.data.offlineHandNumber
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.train-info {
  color: #fff;
  padding: 16px 5px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .item {
    display: flex;
    justify-content: space-between;
    align-items: center;

    &:first-child {
      color: #0ef9b8;

      .icon {
        background-image: url('../../../assets/train-icon.png');
      }
    }
    &:last-child {
      color: #8a8a8a;
      .icon {
        background-image: url('../../../assets/train-icon-disabled.png');
      }
    }
  }
  .icon {
    width: 44px;
    height: 44px;

    background-size: cover;
    margin-right: 12px;
  }

  .label {
    font-size: 12px;
  }

  .value {
    font-weight: bold;
    margin-top: 6px;
    text-align: center;
  }
}

.device-info {
  color: #fff;
  padding: 16px 5px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .item {
    display: flex;
    justify-content: space-between;
    align-items: center;

    &:first-child {
      color: #0ef9b8;

      .icon {
        background-image: url('../../../assets/device-icon.png');
      }
    }
    &:last-child {
      color: #8a8a8a;
      .icon {
        background-image: url('../../../assets/device-icon-disabled.png');
      }
    }
  }
  .icon {
    width: 44px;
    height: 44px;

    background-size: cover;
    margin-right: 12px;
  }

  .label {
    font-size: 12px;
  }

  .value {
    font-weight: bold;
    margin-top: 6px;
    text-align: center;
  }
}
</style>
